# 依赖flask-session库
# 需要启动redis库
# root@b47093a1af74:/data# redis-cli
# 127.0.0.1:6379> key *


from flask import render_template
from flask import Flask,session,request
from flask_session import Session
from redis import Redis

# app=Flask(__name__,static_url_path='',static_folder='')
app =Flask(__name__,static_url_path='',static_folder='templates/static')
app.secret_key='123456'

app.config['SESSION_TYPE']='redis'#指定session类型
app.config['SESSION_PERMANENT']=False #指定为True时，关闭浏览器 session就失效
app.config['SESSION_USE_SIGNER']=False# 是否对发送到浏览器上的session的cookie值 加密

app.config['SESSION_KEY_PREFIX']='session:'#session中值 的前缀
app.config['SESSION_REDIS']=Redis(host='192.168.175.130',port=6379)

Session(app)
#启动测试页面
@app.route('/start')
def tohtml():
    return render_template('login.html')

#获取
@app.route('/get/')
def get():
    if 'username' in session:
        return 'hello,{}'.format(session['username'])
    return 'hello ......'
#保存
@app.route('/login',methods=['POST'])
def login():
    session['username']=request.form['username']
    sessionId=request.cookies.get('session')
    print(sessionId)
    return 'login success ,sessionId ={}'.format(sessionId)

#删除
@app.route('/delete/')
def delete():
    session.pop('username')
    return '删除会话成功'


if __name__=='__main__':
    app.debug=True
    app.run(host='0.0.0.0',port=80)
    # 测试启动login.html